{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0385019b-05ff-4765-9455-9a9a01cd84fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f8aafa4-b56c-4764-aad5-7971ab45c68d",
   "metadata": {},
   "source": [
    "# view / reshape \n",
    "保持tensor大小不变的情况下，各种转换  \n",
    "\n",
    "prod(a.size) = prod(a'.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "74503f0d-9221-41d6-b251-526afc8a93f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.rand(4,1,28,28)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a7dc0189-9c34-468f-bd39-a7272160bb7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([4, 1, 28, 28]), torch.Size([4, 1, 28, 28]))"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape ,  a.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c59698e5-97b4-4b48-8bae-d5a37fc7d313",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2761, 0.4196, 0.4446,  ..., 0.3825, 0.6560, 0.2793],\n",
       "        [0.4631, 0.4090, 0.1243,  ..., 0.7893, 0.7409, 0.9029],\n",
       "        [0.0266, 0.0435, 0.2726,  ..., 0.7674, 0.8863, 0.0996],\n",
       "        [0.5296, 0.6719, 0.0723,  ..., 0.2020, 0.5831, 0.5144]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.view(4,28*28)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c6625b3f-0606-49d6-8948-b506962740e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 784])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.view(4,28*28).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ae46f822-8a03-43d5-b74c-0161333dd2bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2761, 0.4196, 0.4446,  ..., 0.3825, 0.6560, 0.2793],\n",
       "        [0.4631, 0.4090, 0.1243,  ..., 0.7893, 0.7409, 0.9029],\n",
       "        [0.0266, 0.0435, 0.2726,  ..., 0.7674, 0.8863, 0.0996],\n",
       "        [0.5296, 0.6719, 0.0723,  ..., 0.2020, 0.5831, 0.5144]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(4,28*28)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "74d27233-87ff-48fd-9108-f4cfc43c3a8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2761, 0.4196, 0.4446,  ..., 0.3825, 0.6560, 0.2793],\n",
       "        [0.4631, 0.4090, 0.1243,  ..., 0.7893, 0.7409, 0.9029],\n",
       "        [0.0266, 0.0435, 0.2726,  ..., 0.7674, 0.8863, 0.0996],\n",
       "        [0.5296, 0.6719, 0.0723,  ..., 0.2020, 0.5831, 0.5144]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.reshape(a,[4, 784])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b33ff1fd-b6cd-48fc-b80d-fbec311cd00b",
   "metadata": {},
   "source": [
    "# squeeze / unsqueeze\n",
    "张量压缩(减少维度) ，展开维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0091fec0-2910-43ae-8424-d5a247e5b4c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 1, 28, 28])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6f5a2fcc-fba2-45aa-bf52-efa82f6227fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 4, 1, 28, 28])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在第一个位置插入维度\n",
    "a.unsqueeze(0).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0a77fe9d-2c77-4612-89e5-0df6fded3e26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 1, 28, 28, 1])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从后面插入一个维度\n",
    "a.unsqueeze(-1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ce71e409-4e80-460a-8e74-6fba863fa469",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.tensor([1.1,1.2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a25823a7-5023-4292-8661-c316a65dc3e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5180e710-dd25-47d0-8f77-b3a5a1c61a5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 1])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.unsqueeze(-1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ce4e45b2-6b8e-4a68-b842-8227108b324c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.1000],\n",
       "        [1.2000]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.unsqueeze(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1004e4ce-ae2b-4756-afab-0ecb9488a3ee",
   "metadata": {},
   "source": [
    "维度变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "749beacf-7695-44d3-b0ef-7d3c2e886f54",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设需要将b 升维并扩张成f\n",
    "b = torch.rand(32)\n",
    "f = torch.rand(4,32,14,14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a6357749-691d-4cb4-a8a2-20745b89a5ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 32, 1, 1])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#   [32,1]         [32,1,1]      [1,32,1,1]  然后再做升维\n",
    "b = b.unsqueeze(1).unsqueeze(2).unsqueeze(0)\n",
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a7734de2-014f-47d9-84b6-44db6016852d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([32])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 挤压所有维度为1 \n",
    "b.squeeze().shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "866b82ed-0688-4ef5-8c60-c8d9eb897e53",
   "metadata": {},
   "source": [
    "# expand / repeat \n",
    "维度扩展 低维转高维 \n",
    "\n",
    "expand : 只是改变理解方式，不会增加数据 \n",
    "\n",
    "repeat : 会复制数据 \n",
    "\n",
    "区别是否复制数据，推荐expand 不会复制数据 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "053ef881-3a4b-4767-b4f6-114891ffcce6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 32, 14, 14])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.rand(4,32,14,14)\n",
    "# b 由原来的 [1,32,1,1] => [4,32,14,14],expand 扩展只能在原维度为1 做扩张\n",
    "b.expand(4,32,14,14).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dfafec51-77ce-4dee-be4a-c569a7239d34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 32, 1, 1])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -1 代表保持不变\n",
    "b.expand(-1,32,-1,-1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "41eaa74a-068f-4060-beb3-5c0a2cab9a0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 1024, 14, 14])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 每个维度上，代表拷贝次数 ,也就是 原来的dim的重复次数\n",
    "b.repeat(4,32,14,14).shape # [1, 32, 1, 1] => [1*4,32*32,1*14,1*14] = [4, 1024, 14, 14]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecb9564d-8659-444e-9de6-2de03a020869",
   "metadata": {},
   "source": [
    "# transpose / t / permute\n",
    "转置,只能针对矩阵（也就是二维的）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "d76fead3-d6a6-4553-a822-bd614cfb8105",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.rand(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "3d567366-be10-4d67-bea2-ff08f5494034",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.5002, 0.2961, 0.6091, 0.1051],\n",
       "        [0.7182, 0.3928, 0.0151, 0.6128],\n",
       "        [0.3665, 0.7936, 0.3366, 0.9148]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b57746bb-c40e-4a58-83f6-1a2d1a84d965",
   "metadata": {},
   "source": [
    "#### view 无论怎么变化，tensor数据的顺序不会发生变化\n",
    "如下验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9b320cd3-fa16-40ed-a544-4892db81bda5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.5002, 0.2961],\n",
       "         [0.6091, 0.1051],\n",
       "         [0.7182, 0.3928]],\n",
       "\n",
       "        [[0.0151, 0.6128],\n",
       "         [0.3665, 0.7936],\n",
       "         [0.3366, 0.9148]]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a.view(2,3,2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "0717ff7f-d73b-4bc6-b95e-d43c8da768a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.5002, 0.2961, 0.6091, 0.1051],\n",
       "        [0.7182, 0.3928, 0.0151, 0.6128],\n",
       "        [0.3665, 0.7936, 0.3366, 0.9148]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.view(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "7d50a864-a0c3-4ef3-b3f9-4eb37123f3a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.5002, 0.0151],\n",
       "         [0.6091, 0.3665],\n",
       "         [0.7182, 0.3366]],\n",
       "\n",
       "        [[0.2961, 0.6128],\n",
       "         [0.1051, 0.7936],\n",
       "         [0.3928, 0.9148]]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也就是调整了原有tensor的顺序了\n",
    "b.transpose(0,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "060a1498-0a0f-4201-9ea6-daecfc81dc4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.5002, 0.0151],\n",
       "         [0.6091, 0.3665],\n",
       "         [0.7182, 0.3366]],\n",
       "\n",
       "        [[0.2961, 0.6128],\n",
       "         [0.1051, 0.7936],\n",
       "         [0.3928, 0.9148]]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.transpose(0,2).contiguous()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "60adbe21-d17a-4c21-981b-b2457ae9161a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.5002, 0.7182, 0.3665],\n",
       "        [0.2961, 0.3928, 0.7936],\n",
       "        [0.6091, 0.0151, 0.3366],\n",
       "        [0.1051, 0.6128, 0.9148]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.t() # 针对二维的转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5f21fb4b-b613-4fe4-8556-29a777be7284",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.rand(4,3,32,32) # [b,c,h,w] => batch_size channel height  width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "b447e254-8f3d-446d-b308-a0d96b13c3dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 3, 32, 32])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f1d8365-96cd-4d62-9c04-55f02271236d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调换dim的1，3维度\n",
    "[bchw]->[bwhc]        [b whc]\n",
    "a.transpose(1,3).view(4,3*32*32).view(4, 3, 32, 32)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
